﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MFTool
{
    public enum EnumRelationSymbol
    {
        [Description("相等(字符串)")]
        CharEqual,
        [Description("相等(数字)")]
        Equal,
        [Description("相似(Like)")]
        CharLike,
        [Description("大于")]
        Big,
        [Description("大于等于")]
        BigEqual,
        [Description("小于")]
        Less,
        [Description("小于等于")]
        LessEqual,
        [Description("不等于")]
        NoEqual,
        [Description("In")]
        In,
        [Description("期间")]
        DateBetween
    }
    public class QueryBulider
    {
        public int rows { get; set; }
        public int page { get; set; }

        public List<QueryItem> QueryItems { get; set; }

        public string GetWhereString()
        {
            string resStr = "1=1 ";
            if (QueryItems != null)
            {
                foreach (var item in QueryItems)
                {
                    if (item.RelationSymbol == EnumRelationSymbol.CharEqual.ToString())
                    {
                        if (item.FieldValue != null)
                        {
                            resStr += " and {0} = '{1}' ".ReplaceArg(item.FieldName, item.FieldValue.ToString());
                        }
                    }
                    else if (item.RelationSymbol == EnumRelationSymbol.Equal.ToString())
                    {
                        if (item.FieldValue != null)
                        {
                            resStr += " and {0} = {1} ".ReplaceArg(item.FieldName, item.FieldValue.ToString());
                        }
                    }
                    else if (item.RelationSymbol == EnumRelationSymbol.Big.ToString())
                    {
                        if (item.FieldValue != null)
                        {
                            resStr += " and {0} > {1} ".ReplaceArg(item.FieldName, item.FieldValue.ToString());
                        }
                    }
                    else if (item.RelationSymbol == EnumRelationSymbol.BigEqual.ToString())
                    {
                        if (item.FieldValue != null)
                        {
                            resStr += " and {0} >= {1} ".ReplaceArg(item.FieldName, item.FieldValue.ToString());
                        }
                    }
                    else if (item.RelationSymbol == EnumRelationSymbol.Less.ToString())
                    {
                        if (item.FieldValue != null)
                        {
                            resStr += " and {0} < {1} ".ReplaceArg(item.FieldName, item.FieldValue.ToString());
                        }
                    }
                    else if (item.RelationSymbol == EnumRelationSymbol.LessEqual.ToString())
                    {
                        if (item.FieldValue != null)
                        {
                            resStr += " and {0} <= {1} ".ReplaceArg(item.FieldName, item.FieldValue.ToString());
                        }
                    }
                    else if (item.RelationSymbol == EnumRelationSymbol.NoEqual.ToString())
                    {
                        if (item.FieldValue != null)
                        {
                            resStr += " and {0} <> {1} ".ReplaceArg(item.FieldName, item.FieldValue.ToString());
                        }
                    }
                    else if (item.RelationSymbol == EnumRelationSymbol.In.ToString())
                    {
                        if (item.FieldValue != null)
                        {
                            resStr += " and {0} in ({1}) ".ReplaceArg(item.FieldName, item.FieldValue.ToString());
                        }
                    }
                    else if (item.RelationSymbol == EnumRelationSymbol.CharLike.ToString())
                    {                        
                        if (item.FieldValue != null)
                        {
                            resStr += " and {0} like '%{1}%' ".ReplaceArg(item.FieldName, item.FieldValue.ToString());
                        }
                    }
                    else if (item.RelationSymbol == EnumRelationSymbol.DateBetween.ToString())
                    {
                        string tmpStr = "";
                         if (item.FieldValue == null)
                             continue;

                        var fieldValueArr = item.FieldValue.ToString().Split(',');
                        if (fieldValueArr.Length == 2)
                        {
                            if (!string.IsNullOrEmpty(fieldValueArr[0].ToString()))
                            {
                                tmpStr += " {0} >= convert(datetime, '{1}') ".ReplaceArg(item.FieldName, fieldValueArr[0].ToString());
                            }

                            if (!string.IsNullOrEmpty(fieldValueArr[1].ToString()))
                            {
                                tmpStr += " and {0} <= convert(datetime, '{1}') ".ReplaceArg(item.FieldName, fieldValueArr[1].ToString());
                            }
                        }

                        if (!string.IsNullOrEmpty(tmpStr))
                            resStr += " and ({0})".ReplaceArg(tmpStr);
                    }
                }
            }            

            return resStr;
        }
    }

    public class QueryItem
    {
        public string FieldName { get; set; }
        public string RelationSymbol { get; set; }

        public string FieldValue { get; set; }

        public string SearchCtrlType { get; set; }
        
    }
}
